import { _decorator, Button, Color, Component, Label, Node, resources, Scheduler, Sprite, SpriteFrame, tween, v3 } from 'cc';
import { LayerBase } from '../utils/LayerBase';
import { Main } from './Main';
import { randomNum } from '../utils/utils';
import { AudioMgr } from '../utils/AudioMgr';
import { UILayerMgr } from '../utils/UILayerMgr';
import { UserInfo } from '../UserInfo';
const { ccclass, property } = _decorator;

@ccclass('GateGamePanel')
export class GateGamePanel extends LayerBase {
    @property(Label)
    countDownLabel:Label;
    @property(Label)
    gateLevelLabel:Label;
    @property(Node)
    songBtnRoot:Node;
    @property(Label)
    songAmountLabel:Label;

    static ins:GateGamePanel=null

    isGameIng;

    songTotalAmount=5;//总共几首歌
    private currSongAmount;//当前是第几首歌

    get CurrSongAmount() {
        return this.currSongAmount;
    }
    set CurrSongAmount(value) {
        this.currSongAmount = value;
        this.songAmountLabel.string = `${value}/${this.songTotalAmount}`
    }


    disPlayBtnSongCongfiArr=[];//下个四个按钮对应的歌配置

    private currSongTypeNum;//当前是啥歌，答案

    get CurrSongTypeNum() {//答案
        return this.currSongTypeNum;
    }
    set CurrSongTypeNum(value) {
        this.currSongTypeNum = value;

        console.log("播放歌：",Main.ins.songCongfiDic.get(this.currSongTypeNum).name)
        
        AudioMgr.ins.play(Main.ins.songCongfiDic.get(this.currSongTypeNum).path);
    }

    maxCoutDownNum=10;
    tempCoutDownNum=0;
    private coutDownNum;

    get CoutDownNum() {
        return this.coutDownNum;
    }
    set CoutDownNum(value) {
        if (value<0) {//倒计时结束，重新倒计时
            console.log("倒计时结束")
            this.gameOver();
            return;
        }
        this.coutDownNum = Math.round(value);
        this.countDownLabel.string=this.coutDownNum+"";

    }

    protected onLoad(): void {
        GateGamePanel.ins=this;
    }

    init() {
        this.initUI();
        this.gameStart();
    }

    initUI(){
        this.gateLevelLabel.string="关卡:"+UserInfo.gateLevel;
    }

    gameStart(){
        this.CoutDownNum=this.maxCoutDownNum;
        this.CurrSongAmount=1;
        this.isGameIng=true;

        let spaceSongTypeArr = [];//所有可用的type
        for (let i = 0; i < Main.ins.songCongfiDic.length; i++) {
            spaceSongTypeArr.push(i);
        }
        let type = spaceSongTypeArr[randomNum(0, spaceSongTypeArr.length - 1)];
        this.CurrSongTypeNum = type;
        this.renewSongBtns();
       
    }

    nextSong() {
        if (this.CurrSongAmount >= this.songTotalAmount) {//判断游戏是否进入下一关
            this.nextGateLevel();
        }
        else{
            this.CoutDownNum=this.maxCoutDownNum;
    
            let spaceSongTypeArr = [];//所有可用的type
            for (let i = 0; i < Main.ins.songCongfiDic.length; i++) {
                spaceSongTypeArr.push(i);
            }
            let type = spaceSongTypeArr[randomNum(0, spaceSongTypeArr.length - 1)];
            this.CurrSongTypeNum = type;
    
            this.renewSongBtns();
            this.CurrSongAmount += 1;
        }
    }

    nextGateLevel() {
        UserInfo.gateLevel += 1;
        UserInfo.taskTodayWinGateNum+=1;
        Main.ins.saveGameData();
        this.init();
    }


    onclickSongBtn(event, index) {

        let btnNode = this.songBtnRoot.children[parseInt(index)];

        if (index == this.currSongPosIndex) {//是正确答案
            console.log("点击了正确答案")

            btnNode.getComponent(Sprite).color = Color.BLUE;
        
            this.scheduleOnce(() => {
                this.nextSong();
            }, 1)

        }
        else {
            console.log("点击了错误答案")

            btnNode.getComponent(Sprite).color = Color.RED;

            this.scheduleOnce(() => {
                this.gameOver();
            }, 1)

        }

        for (let i = 0; i < 4; i++) {
            this.songBtnRoot.children[i].getComponent(Button).enabled = false;
        }


    }

    gameOver() {
        if (this.isGameIng == true) {
            this.isGameIng = false;
            AudioMgr.ins.stop();
            UILayerMgr.getIns.openUILayer("gateFailPanel");
        }
    }

    revive(){
        this.isGameIng=true;

        this.CoutDownNum=this.maxCoutDownNum;

        let spaceSongTypeArr = [];//所有可用的type
        for (let i = 0; i < Main.ins.songCongfiDic.length; i++) {
            spaceSongTypeArr.push(i);
        }
        let type = spaceSongTypeArr[randomNum(0, spaceSongTypeArr.length - 1)];
        this.CurrSongTypeNum = type;
        this.renewSongBtns();
    }

    protected update(dt: number): void {
        if (this.isGameIng) {
            this.tempCoutDownNum+=dt;
            if (this.tempCoutDownNum>=1) {
                this.tempCoutDownNum=0;
                this.CoutDownNum-=1;
            }
          
        }
      
    }

    //更新四个歌按钮
    renewSongBtns() {
        this.disPlayBtnSongCongfiArr=[];
        let spaceSongTypeArr = [];//所有可用的type，除答案
        for (let i = 0; i < Main.ins.songCongfiDic.length; i++) {
            if (i==this.currSongTypeNum) {
                continue;
            }
            spaceSongTypeArr.push(i);
        }
        console.log(spaceSongTypeArr,"spaceSongTypeArr")

        let randcount=randomNum(0,3);//正确答案的顺序

        for (let t = 0; t < 4; t++) {
            if (randcount == t) {
                this.disPlayBtnSongCongfiArr.push(Main.ins.songCongfiDic.get(this.currSongTypeNum));
                continue;
            }
            else {
                let type = spaceSongTypeArr[randomNum(0, spaceSongTypeArr.length - 1)];
                spaceSongTypeArr.splice(spaceSongTypeArr.indexOf(type), 1);

                this.disPlayBtnSongCongfiArr.push(Main.ins.songCongfiDic.get(type));
            }

        }

        console.log(this.disPlayBtnSongCongfiArr,"this.disPlayBtnSongCongfiArr")
        for (let i = 0; i < 4; i++) {
            this.songBtnRoot.children[i].getChildByName("Label").getComponent(Label).string=this.disPlayBtnSongCongfiArr[i].name;
        }

        for (let i = 0; i < 4; i++) {
            this.songBtnRoot.children[i].getComponent(Button).enabled=true;
            this.songBtnRoot.children[i].getChildByName("sp").active=false;
            this.songBtnRoot.children[i].getComponent(Sprite).color=Color.WHITE;
        }

        this.songBtnRoot.setPosition(0,-670,0);
        tween(this.songBtnRoot)
        .to(0.5,{position:v3(0,40,0)},{easing:"circIn"})
        .start()


    }


    get currSongPosIndex(){//当前歌答案的顺序

        let str=Main.ins.songCongfiDic.get(this.currSongTypeNum).name;

        let posIndex=0;
        for (let i = 0; i < 4; i++) {
            if (this.songBtnRoot.children[i].getChildByName("Label").getComponent(Label).string==str) {
                posIndex=i;
            }
        }

        return posIndex;

    }

    onclickCloseBtn(): void {
        AudioMgr.ins.stop();
        UILayerMgr.getIns.closeUILayer(this.node.name);

    }

}


